package com.qf.channel;

import com.qf.entity.Msg;
import com.qf.util.ChannelUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@ChannelHandler.Sharable
@Order(100)
public class MsgRegisterChannelHandler extends SimpleChannelInboundHandler<Msg> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Msg msg) throws Exception {
        if (msg.getType() == 1) {
            System.out.println("接收到注册消息：" + msg);
            //注册消息
            String devid = (String) msg.getData();//设备号
            Channel channel = ctx.channel();//对应客户端的channel
            //构建关联关系
            ChannelUtil.putChannel(devid, channel);
        } else {
            ctx.fireChannelRead(msg);
        }
    }
}
